package cn.wxxyhw.Dao;

import cn.wxxyhw.Entity.User;
import cn.wxxyhw.Util.DataSourceUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.stereotype.Repository;
import java.sql.SQLException;
import java.util.List;

/**
 * 用户数据操作
 * @Author: 洪稳
 * @Date: 2019/6/3
 */
@Repository
public class UserDao {
    static QueryRunner queryRunner ;
    static {
        queryRunner = new QueryRunner(DataSourceUtil.getDataSource());
    }

    /**
     * @param username 用户id
     * @param password 用户密码
     * @return User实体
     */
    public User login(String username,String password){
        String sql = "select * from user where uid = ? and password = ?";
        User user = null;
        try {
            user = queryRunner.query(sql, new BeanHandler<>(User.class), username, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    /**
     * 检验用户是否已经存在
     * @param id
     * @return
     */
    public User userIsExit(String id){
        String sql = "select * from user where uid = ?";
        User user = null;
        try {
            user = queryRunner.query(sql, new BeanHandler<>(User.class), id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }

    /**
     * 注册user
     * @param user
     */
    public int regeist(User user) {
        int update = 0;
        String sql = "insert into user(uid,email,password,username,image) values(?,?,?,?,?)";
        try {
            update = queryRunner.update(sql, user.getUid(), user.getEmail(), user.getPassword(), user.getUsername(),user.getImage());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return update;
    }

    /**
     * 根据名字查询个人图片
     * @param name
     * @return
     */
    public String getImageByName(String name) {
        String sql = "select * from user where uid = ?";
        User user = null;
        try {
             user = queryRunner.query(sql, new BeanHandler<>(User.class), name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (user!=null && null!=user.getImage() && !"".equals(user.getImage())){
            return user.getImage();
        }
        return "/static/image/sum.jpg"; //默认的图片
    }

    /**
     * 查询全部用户
     * @return
     */
    public List<User> getUsers() {
        List<User> users = null;
        String sql = "select * from user";
        try {
            users = queryRunner.query(sql, new BeanListHandler<>(User.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return users;
    }

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    public User getUserById(int id) {
        User user=null;
        String sql = "select * from user where id = ?";
        try {
            user = queryRunner.query(sql, new BeanHandler<>(User.class), id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        user.setPassword(null);
        return user;
    }

    /**
     * 更新数据
     * @param uid
     * @param nickname
     * @param email
     * @param image
     */
    public void update(String uid, String nickname, String email, String image) {
        String sql = "update user set username = ? ,email = ? ,image = ? where uid = ?";
        if (image==null){
            sql = "update user set username = ? ,email = ? where uid = ?";
        }
        try {
            queryRunner.update(sql,nickname,email,image,uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询用户密码
     * @param id
     * @return
     */
    public String getPasswordById(int id) {
        User user=null;
        String sql = "select * from user where id = ?";
        try {
            user = queryRunner.query(sql, new BeanHandler<>(User.class), id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user.getPassword();
    }

    /**
     * 查询用户密码
     * @param id
     * @return
     */
    public String getPasswordById(String id) {
        User user=null;
        String sql = "select * from user where uid = ?";
        try {
            user = queryRunner.query(sql, new BeanHandler<>(User.class), id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user.getPassword();
    }

    /**
     * 修改密码
     * @param pw 新密码
     * @param id id
     */
    public void updatePassword(String pw, int id) {
        String sql = "update user set password = ? where id = ?";
        try {
            queryRunner.update(sql,pw,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    /**
     * 通过id查询用户
     * @param friendId
     * @return
     */
    public User getUserByUid(String friendId) {
        User user=null;
        String sql = "select * from user where uid = ?";
        try {
            user = queryRunner.query(sql, new BeanHandler<>(User.class), friendId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        user.setPassword(null);
        return user;
    }

    /**
     * 通过name查询id
     * @param name
     * @return
     */
    public String getUserIdByName(String name) {
        User user=null;
        String sql = "select * from user where username = ?";
        try {
            user = queryRunner.query(sql, new BeanHandler<>(User.class), name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user.getUid();
    }

    /**
     * 通过name查询id
     * @param id
     * @return
     */
    public String getUserNameById(String id) {
        User user=null;
        String sql = "select * from user where uid = ?";
        try {
            user = queryRunner.query(sql, new BeanHandler<>(User.class), id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user.getUsername();
    }

    /**
     * 更新数据
     * @param uid
     * @param nickname
     * @param image
     */
    public void dataUpdate(String uid, String nickname, String image) {
        String[] sql = new String[5];

        if (null!=image){
            sql[0] = "update article set image = ? where uid = ?";
            sql[1] = "update comment set fromName = ? ,fromImage = ? where fromId = ?";
            sql[2] = "update comment set toName = ? where toId = ?";
            sql[3] = "update liuyan set fromName = ? ,fromImage = ? where fromId = ?";
            sql[4] = "update liuyan set toName = ? where toId = ?";
            try {
                queryRunner.update(sql[0],image,uid);
                queryRunner.update(sql[1],nickname,image,uid);
                queryRunner.update(sql[2],nickname,uid);
                queryRunner.update(sql[3],nickname,image,uid);
                queryRunner.update(sql[4],nickname,uid);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (null==image){
            sql[1] = "update comment set fromName = ? where fromId = ?";
            sql[2] = "update comment set toName = ? where toId = ?";
            sql[3] = "update liuyan set fromName = ?  where fromId = ?";
            sql[4] = "update liuyan set toName = ? where toId = ?";
            try {
                queryRunner.update(sql[1],nickname,uid);
                queryRunner.update(sql[2],nickname,uid);
                queryRunner.update(sql[3],nickname,uid);
                queryRunner.update(sql[4],nickname,uid);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 确认信息
     * @param id 用户id
     * @param email 用户邮箱
     * @return
     */
    public boolean sureMsg(String id, String email) {
        User user=null;
        String sql = "select * from user where uid = ? and email = ?";
        try {
            user = queryRunner.query(sql, new BeanHandler<>(User.class), id,email);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (user!=null && user.getUid()==id){
            return true;
        }
        return false;
    }
}

